function varargout = gui(varargin)
% GUI M-file for gui.fig
%      GUI, by itself, creates a new GUI or raises the existing
%      singleton*.
%
%      H = GUI returns the handle to a new GUI or the handle to
%      the existing singleton*.
%
%      GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI.M with the given input arguments.
%
%      GUI('Property','Value',...) creates a new GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gui

% Last Modified by GUIDE v2.5 02-Feb-2012 20:04:29

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before gui is made visible.
function gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to gui (see VARARGIN)

Roboter_Init; %Roboter Initialisierung
positionsbestimmung; % Breite und Länge des zu durchsuchenden Feldes werden bestimmt

% Choose default command line output for gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

global hStop_manuell;
global hZurueck_manuell;
global hLinks_manuell;
global hRechts_manuell;
global hVor_manuell;
global hGreifer_oeffnen;
global hGreifer_Schliessen;
global hreturn2start;
global hSuche_starten;
global GUI_x;
global GUI_y;


hSuche_starten = findobj('tag','Suche_starten');
hStop_manuell = findobj('tag','Stop_manuell');
hZurueck_manuell = findobj('tag','Zurueck_manuell');
hLinks_manuell = findobj('tag','Links_manuell');
hRechts_manuell = findobj('tag','Rechts_manuell');
hVor_manuell = findobj('tag','Vor_manuell');
hGreifer_oeffnen = findobj('tag','Greifer_oeffnen');
hGreifer_Schliessen = findobj('tag','Greifer_Schliessen');
hreturn2start = findobj('tag','return2start');
GUI_x = findobj('tag','X_Wert');
GUI_y = findobj('tag','Y_Wert');


% UIWAIT makes gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = gui_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in Suche_starten. (Startsuche)
function Suche_starten_Callback(hObject, eventdata, handles)
global abgesucht_x;
global abgesucht_y;
% hObject    handle to Suche_starten (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

disp('Suche gestartet');

openbutton = findobj('tag','Suche_starten');
closebutton1 = findobj('tag','Stop_manuell');
closebutton2 = findobj('tag','Zurueck_manuell');
closebutton3 = findobj('tag','Links_manuell');
closebutton4 = findobj('tag','Rechts_manuell');
closebutton5 = findobj('tag','Vor_manuell');
closebutton6 = findobj('tag','Greifer_oeffnen');
closebutton7 = findobj('tag','Greifer_Schliessen');
closebutton8 = findobj('tag','return2start');

set(openbutton,'Enable','off');
set(closebutton1,'Enable','off');
set(closebutton2,'Enable','off');
set(closebutton3,'Enable','off');
set(closebutton4,'Enable','off');
set(closebutton5,'Enable','off');
set(closebutton6,'Enable','off');
set(closebutton7,'Enable','off');
set(closebutton8,'Enable','off');

if (abgesucht_x== -1 && abgesucht_y == -1)
start_search();
else
    return2position;
    start_search;
    end

gui_reset;


% --- Executes on button press in return2start.
function return2start_Callback(hObject, eventdata, handles)
% hObject    handle to return2start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

disp('Zurück zum Start');

openbutton = findobj('tag','return2start');
closebutton1 = findobj('tag','Stop_manuell');
closebutton2 = findobj('tag','Zurueck_manuell');
closebutton3 = findobj('tag','Links_manuell');
closebutton4 = findobj('tag','Rechts_manuell');
closebutton5 = findobj('tag','Vor_manuell');
closebutton6 = findobj('tag','Greifer_oeffnen');
closebutton7 = findobj('tag','Greifer_Schliessen');
closebutton8 = findobj('tag','Suche_starten');

set(openbutton,'Enable','off');
set(closebutton1,'Enable','off');
set(closebutton2,'Enable','off');
set(closebutton3,'Enable','off');
set(closebutton4,'Enable','off');
set(closebutton5,'Enable','off');
set(closebutton6,'Enable','off');
set(closebutton7,'Enable','off');
set(closebutton8,'Enable','off');

return2start();

gui_reset;


% --- Executes on button press in Greifer_oeffnen.
function Greifer_oeffnen_Callback(hObject, eventdata, handles)
% hObject    handle to Greifer_oeffnen (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

disp('Greifer geöffnet');

openbutton = findobj('tag','Greifer_oeffnen');
closebutton = findobj('tag','Greifer_Schliessen');

set(openbutton,'Enable','off');
greifer_open();
set(closebutton,'Enable','on');




% --- Executes on button press in Greifer_Schliessen.
function Greifer_Schliessen_Callback(hObject, eventdata, handles)
% hObject    handle to Greifer_Schliessen (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

disp('Greifer geschlossen');

openbutton = findobj('tag','Greifer_Schliessen');
closebutton = findobj('tag','Greifer_oeffnen');

set(openbutton,'Enable','off');
greifer_close();
set(closebutton,'Enable','on');




% --- Executes on button press in Vor_manuell.
function Vor_manuell_Callback(hObject, eventdata, handles)
% hObject    handle to Vor_manuell (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

openbutton = findobj('tag','Vor_manuell');
closebutton1 = findobj('tag','Stop_manuell');
closebutton2 = findobj('tag','Zurueck_manuell');
closebutton3 = findobj('tag','Links_manuell');
closebutton4 = findobj('tag','Rechts_manuell');
closebutton5 = findobj('tag','Suche_starten');
closebutton6 = findobj('tag','return2start');

set(openbutton,'Enable','off');
set(closebutton1,'Enable','on');
set(closebutton2,'Enable','off');
set(closebutton3,'Enable','off');
set(closebutton4,'Enable','off');
set(closebutton5,'Enable','off');
set(closebutton6,'Enable','off');

engine_forwards();


% --- Executes on button press in Stop_manuell.
function Stop_manuell_Callback(hObject, eventdata, handles)
% hObject    handle to Stop_manuell (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

openbutton = findobj('tag','Stop_manuell');
closebutton1 = findobj('tag','Vor_manuell');
closebutton2 = findobj('tag','Zurueck_manuell');
closebutton3 = findobj('tag','Links_manuell');
closebutton4 = findobj('tag','Rechts_manuell');
closebutton5 = findobj('tag','Suche_starten');
closebutton6 = findobj('tag','Suche_starten');
closebutton7 = findobj('tag','return2start');

set(openbutton,'Enable','off');
set(closebutton1,'Enable','on');
set(closebutton2,'Enable','on');
set(closebutton3,'Enable','on');
set(closebutton4,'Enable','on');
set(closebutton5,'Enable','on');
set(closebutton6,'Enable','on');
set(closebutton7,'Enable','on');

engine_stop();


% --- Executes on button press in Zurueck_manuell.
function Zurueck_manuell_Callback(hObject, eventdata, handles)
% hObject    handle to Zurueck_manuell (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

openbutton = findobj('tag','Zurueck_manuell');
closebutton1 = findobj('tag','Stop_manuell');
closebutton2 = findobj('tag','Vor_manuell');
closebutton3 = findobj('tag','Links_manuell');
closebutton4 = findobj('tag','Rechts_manuell');
closebutton5 = findobj('tag','Suche_starten');
closebutton6 = findobj('tag','return2start');

set(openbutton,'Enable','off');
set(closebutton1,'Enable','on');
set(closebutton2,'Enable','off');
set(closebutton3,'Enable','off');
set(closebutton4,'Enable','off');
set(closebutton5,'Enable','off');
set(closebutton6,'Enable','off');

engine_backwards();


% --- Executes on button press in Rechts_manuell.
function Rechts_manuell_Callback(hObject, eventdata, handles)
% hObject    handle to Rechts_manuell (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

openbutton = findobj('tag','Rechts_manuell');
closebutton1 = findobj('tag','Stop_manuell');
closebutton2 = findobj('tag','Vor_manuell');
closebutton3 = findobj('tag','Links_manuell');
closebutton4 = findobj('tag','Zurueck_manuell');
closebutton5 = findobj('tag','Suche_starten');
closebutton6 = findobj('tag','return2start');

set(openbutton,'Enable','off');
set(closebutton1,'Enable','on');
set(closebutton2,'Enable','off');
set(closebutton3,'Enable','off');
set(closebutton4,'Enable','off');
set(closebutton5,'Enable','off');
set(closebutton6,'Enable','off');

engine_right();


% --- Executes on button press in Links_manuell.
function Links_manuell_Callback(hObject, eventdata, handles)
% hObject    handle to Links_manuell (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

openbutton = findobj('tag','Links_manuell');
closebutton1 = findobj('tag','Stop_manuell');
closebutton2 = findobj('tag','Vor_manuell');
closebutton3 = findobj('tag','Rechts_manuell');
closebutton4 = findobj('tag','Zurueck_manuell');
closebutton5 = findobj('tag','Suche_starten');
closebutton6 = findobj('tag','return2start');

set(openbutton,'Enable','off');
set(closebutton1,'Enable','on');
set(closebutton2,'Enable','off');
set(closebutton3,'Enable','off');
set(closebutton4,'Enable','off');
set(closebutton5,'Enable','off');
set(closebutton6,'Enable','off');

engine_left();


% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

COM_CloseNXT('all');
